package progetto3.managerAB;

public abstract class ManagerAB implements Manager {
	
	protected static final int DIM=2;
	protected static final int MAXP = DIM*2;
	protected int [] cnt = new int[DIM];
	protected int cntp;
	protected boolean busy;
	protected int w;//thread in wait
	
	protected boolean canPass(MyRunnable r){
		return cnt[r.getId()]<2 && !busy;
	}
	
	protected void entered(MyRunnable r){
		busy=true;
		cnt[r.getId()]++;
		cntp++;
	}
	
	protected void exited(MyRunnable r){
		busy=false;
		if(cnt[r.getId()]==1 && cnt[1-r.getId()]==1){
			cntp=0;
			cnt[r.getId()]=0; cnt[1-r.getId()]=0;
		}
		if(cntp==MAXP){
			for (int i = 0; i < cnt.length; i++) {
				cnt[i]=0;
			}
			cntp=0;
		}
	}
}
